/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import pojo.Deal;
import java.util.ArrayList;
import java.util.Date;
import pojo.Thanhpho;
import until.MySqlDataAccessHelper;

/**
 *
 * @author Administrator
 */
public class Deal_DAO {

    public static ArrayList<Deal> layDSDealGanDay() {
        ArrayList<Deal> ds = new ArrayList<Deal>();
        String sql = String.format("select * from deal limit 6");
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                Deal deal = new Deal();
                deal.setId(rs.getInt("ID"));
                deal.setHinhDaiDien(rs.getString("HinhDaiDien"));
                deal.setGiaThuong(rs.getDouble("GiaThuong"));
                deal.setGiaDaGiam(rs.getDouble("GiaDaGiam"));
                deal.setTrangThai(rs.getString("TrangThai"));
                deal.setTrangThaiBanDeal(rs.getString("TrangThaiBanDeal"));
                deal.setNgayBatDau(rs.getDate("NgayBatDau"));
                deal.setNgayKetThuc(rs.getDate("NgayKetThuc"));
                deal.setSoLuongDeal(rs.getInt("SoLuongDeal"));
                deal.setSoLuongDealToiThieu(rs.getInt("SoLuongDealToiThieu"));
                deal.setSoLuongDealTrenMotNguoi(rs.getInt("SoLuongDealTrenMotNguoi"));
                deal.setTileSmallDeal(rs.getString("TileSmallDeal"));
                deal.setTieuDeDeal(rs.getString("TieuDeDeal"));
                deal.setDacDiemNoiBat(rs.getString("DacDiemNoiBat"));
                deal.setDieuKien(rs.getString("DieuKien"));
                deal.setNoiDungDeal(rs.getString("NoiDungDeal"));
                deal.setNgayTaoDeal(rs.getDate("NgayTaoDeal"));
                deal.setSoNguoiDaXem(rs.getInt("SoNguoiDaXem"));
                ds.add(deal);
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        return ds;
    }

    public static ArrayList<Deal> layDealBenTrai() {
        ArrayList<Deal> ds = new ArrayList<Deal>();
        String sql = String.format("select * from deal limit 4");
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                Deal deal = new Deal();
                deal.setId(rs.getInt("ID"));
                deal.setHinhDaiDien(rs.getString("HinhDaiDien"));
                deal.setGiaThuong(rs.getDouble("GiaThuong"));
                deal.setGiaDaGiam(rs.getDouble("GiaDaGiam"));
                deal.setTrangThai(rs.getString("TrangThai"));
                deal.setTrangThaiBanDeal(rs.getString("TrangThaiBanDeal"));
                deal.setNgayBatDau(rs.getDate("NgayBatDau"));
                deal.setNgayKetThuc(rs.getDate("NgayKetThuc"));
                deal.setSoLuongDeal(rs.getInt("SoLuongDeal"));
                deal.setSoLuongDealToiThieu(rs.getInt("SoLuongDealToiThieu"));
                deal.setSoLuongDealTrenMotNguoi(rs.getInt("SoLuongDealTrenMotNguoi"));
                deal.setTileSmallDeal(rs.getString("TileSmallDeal"));
                deal.setTieuDeDeal(rs.getString("TieuDeDeal"));
                deal.setDacDiemNoiBat(rs.getString("DacDiemNoiBat"));
                deal.setDieuKien(rs.getString("DieuKien"));
                deal.setNoiDungDeal(rs.getString("NoiDungDeal"));
                deal.setNgayTaoDeal(rs.getDate("NgayTaoDeal"));
                deal.setSoNguoiDaXem(rs.getInt("SoNguoiDaXem"));
                ds.add(deal);
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        return ds;
    }

    public static int LaySoLuongSanPham(int maDanhMuc) {
        String sql = String.format("select count(*) as SoLuong from Deal where MaDanhMuc=" + maDanhMuc + "");
        int soLuong = -1;
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            if (rs.next()) {
                soLuong = rs.getInt("SoLuong");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return soLuong;
    }

    public static int tinhSoLuongTrang(int maDanhMuc) {
        int soTrang;
        int soSanPhamTrenTrang = 6;
        int soLuong = Deal_DAO.LaySoLuongSanPham(maDanhMuc);
        soTrang = soLuong / soSanPhamTrenTrang;
        if (soLuong % soSanPhamTrenTrang != 0) {
            soTrang++;
        }
        return soTrang;
    }

    public static ArrayList<Deal> laySanPhamTheoDanhMucCoPhanTrang(int maDanhMuc, int trang) {
        ArrayList<Deal> ds = new ArrayList<Deal>();
        int soSanPhamTrenTrang = 6;
        int batDau = (trang - 1) * soSanPhamTrenTrang;
        String sql = String.format("select * from deal as deal where deal.MaDanhMuc=" + maDanhMuc + " LIMIT " + batDau + "," + soSanPhamTrenTrang + "");
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                Deal deal = new Deal();
                deal.setId(rs.getInt("ID"));
                deal.setHinhDaiDien(rs.getString("HinhDaiDien"));
                deal.setGiaThuong(rs.getDouble("GiaThuong"));
                deal.setGiaDaGiam(rs.getDouble("GiaDaGiam"));
                deal.setTrangThai(rs.getString("TrangThai"));
                deal.setTrangThaiBanDeal(rs.getString("TrangThaiBanDeal"));
                deal.setNgayBatDau(rs.getDate("NgayBatDau"));
                deal.setNgayKetThuc(rs.getDate("NgayKetThuc"));
                deal.setSoLuongDeal(rs.getInt("SoLuongDeal"));
                deal.setSoLuongDealToiThieu(rs.getInt("SoLuongDealToiThieu"));
                deal.setSoLuongDealTrenMotNguoi(rs.getInt("SoLuongDealTrenMotNguoi"));
                deal.setTileSmallDeal(rs.getString("TileSmallDeal"));
                deal.setTieuDeDeal(rs.getString("TieuDeDeal"));
                deal.setDacDiemNoiBat(rs.getString("DacDiemNoiBat"));
                deal.setDieuKien(rs.getString("DieuKien"));
                deal.setNoiDungDeal(rs.getString("NoiDungDeal"));
                deal.setNgayTaoDeal(rs.getDate("NgayTaoDeal"));
                deal.setSoNguoiDaXem(rs.getInt("SoNguoiDaXem"));
                ds.add(deal);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return ds;
    }

    public static Deal layMotDealMau(int maDeal) {
        Deal deal = new Deal();
        String sql = "SELECT * from deal,congty WHERE deal.ID=N'" + maDeal + "' and deal.MaCongTy=congty.ID";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while (rs.next()) {
                deal.setId(rs.getInt("ID"));
                deal.setHinhDaiDien(rs.getString("HinhDaiDien"));
                deal.setGiaThuong(rs.getDouble("GiaThuong"));
                deal.setGiaDaGiam(rs.getDouble("GiaDaGiam"));
                deal.setTrangThai(rs.getString("TrangThai"));
                deal.setTrangThaiBanDeal(rs.getString("TrangThaiBanDeal"));
                deal.setNgayBatDau(rs.getDate("NgayBatDau"));
                deal.setNgayKetThuc(rs.getDate("NgayKetThuc"));
                deal.setSoLuongDeal(rs.getInt("SoLuongDeal"));
                deal.setSoLuongDealToiThieu(rs.getInt("SoLuongDealToiThieu"));
                deal.setSoLuongDealTrenMotNguoi(rs.getInt("SoLuongDealTrenMotNguoi"));
                deal.setTileSmallDeal(rs.getString("TileSmallDeal"));
                deal.setTieuDeDeal(rs.getString("TieuDeDeal"));
                deal.setDacDiemNoiBat(rs.getString("DacDiemNoiBat"));
                deal.setDieuKien(rs.getString("DieuKien"));
                deal.setNoiDungDeal(rs.getString("NoiDungDeal"));
                deal.setNgayTaoDeal(rs.getDate("NgayTaoDeal"));
                deal.setSoNguoiDaXem(rs.getInt("SoNguoiDaXem"));
                deal.setTenCongTy(rs.getString("TenCongTy"));
                deal.setWebSite(rs.getString("Website"));
                deal.setDiaChi(rs.getString("DiaChi"));
                deal.setDienThoai(rs.getString("DienThoai"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return deal;
    }
    // Hàm này trả về 
    // 0 nếu Deal thỏa diều kiện và vẫn đang được bán
    // 1 nếu Deal này hết hạn
    // 2 nếu Deal này cháy hàng
    // chay hang khi so deal duoc mua = tong so deal ban va ngay hien tai < ngay het han return 2

    public static int TrangThaiDeal(int maDeal) {
        int flag = 0;
        Date todayD = new Date(System.currentTimeMillis());
        Deal deal= layMotDealMau(maDeal);
        Date ngayHetHanDeal=deal.getNgayKetThuc();
        if(ngayHetHanDeal.before(todayD)){
            flag=1;
        }else{
            int tongSoDealDaDatMua=DonDatHang_DAO.demSoDealTrongDonDatHangTheoMaDeal(maDeal);
            int tongSoDeal=deal.getSoLuongDeal();
            if(tongSoDealDaDatMua==tongSoDeal&&todayD.before(ngayHetHanDeal)){
                return flag=3;
            }
            flag=0;
        }
        return flag;
    }
    // ham này sẽ điêu kiển thanh status bar thể hiện cần bào nhiêu người để được giá tốt và thông báo đã đạt đc giá tốt hay chưa ??
    public static int kiemTraTrangThaiBanDeal(int maDeal){
        int flag=0;
        Deal deal= layMotDealMau(maDeal);
        int tongSoDealDaDatMua=DonDatHang_DAO.demSoDealTrongDonDatHangTheoMaDeal(maDeal);
        int soLuongDealToiThieuDeDuocGiaTot=deal.getSoLuongDealToiThieu();
        if(tongSoDealDaDatMua<soLuongDealToiThieuDeDuocGiaTot){
            flag=1;
        }
        return  flag;
    }
    
}
